package com.huitone.smspfm.context.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
* @Title: AppManager.java 
* @Package com.unicom.gddw.app 
* @Description: TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年3月12日 下午11:53:59 
* @version V1.0
 */
public class AppManager {
	
	public static enum AppStatus {STARTING, RUNNING, SHUTDOWN, RELOAD, RELOADING};
	
	public static AppStatus currAppStatus = AppStatus.RUNNING;
	
	/**
	 * 记录所有线程的一次运行状态是否完成，false表示线程开始执行，true表示线程一轮执行完成
	 */
	public static Map<String, Boolean> threadExcuSucc = new HashMap<String, Boolean>();
	
	/**
	 * 登记所有线程池实例
	 */
	public static List<ExecutorService> esRegister = new ArrayList<ExecutorService>();
	
	public static boolean isRunning() {
		
		boolean result = true;
		
		if (currAppStatus == AppStatus.SHUTDOWN) {
			result = false;
		}
		
		return result;
	}
	
	public static boolean isReloading() {
		
		boolean result = false;
		
		if (currAppStatus == AppStatus.RELOAD) {
			result = true;
		}
		
		return result;
	}
	
	public static boolean isShutdown() {
		
		boolean result = false;
		
		if (currAppStatus == AppStatus.SHUTDOWN) {
			result = true;
		}
		
		return result;
	}
	
	public synchronized static void starting() {
		currAppStatus = AppStatus.STARTING;
	}
	
	public synchronized static void running() {
		currAppStatus = AppStatus.RUNNING;
	}
	
	public synchronized static void shutdown() {
		currAppStatus = AppStatus.SHUTDOWN;
	}
	
	public synchronized static void reload() {
		currAppStatus = AppStatus.RELOAD;
	}
	
	public synchronized static void reloading() {
		currAppStatus = AppStatus.RELOADING;
	}
	
	public static boolean isAllEsShutdowned() {
		
		boolean allshutdowned = true;
		
		for (ExecutorService es:esRegister) {
			if (es != null) {
				boolean shutdowned = es.isShutdown();
				if (!shutdowned) {
					allshutdowned = false;
					break;
				}
			}
			
		}
		
		return allshutdowned;
	}
}
